// BSMain.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "BFsol.h"
#include "BSJniApi.h"

SOL_DRIVER *g_sol = NULL;
void UnicodeToAsc(USHORT* input, char* output);
void ReleaseSOL();

JNIEXPORT jstring JNICALL Java_com_wiseman_writing_utility_BSJniApi_load(JNIEnv *env, jobject job,
		jobject fileJob, jlong offset, jint runMode, jint codePage) {
	jclass fdClass = (*env)->FindClass(env, "java/io/FileDescriptor");

	if (fdClass != NULL) {
		jclass fdClassRef = (jclass)(*env)->NewGlobalRef(env, fdClass);
		jfieldID fdClassDescriptorFieldID = (*env)->GetFieldID(env, fdClass,
				"descriptor", "I");

		if (fdClassDescriptorFieldID != NULL && fileJob != NULL) {
			jint fd = (*env)->GetIntField(env, fileJob,
					fdClassDescriptorFieldID);
			int myfd = dup(fd);
			FILE *myFile = fdopen(myfd, "rb");
			if (myFile) {
				g_sol = solInit(myFile, offset, runMode, codePage);
				if (g_sol != NULL) {
					LOGD("load success!");
					return (*env)->NewStringUTF(env, "sucess!");
				}
			}
		}
	}
	return NULL;
}
JNIEXPORT jint JNICALL Java_com_wiseman_writing_utility_BSJniApi_prepareData(JNIEnv *env, jobject job,
		jchar code) {
	if (g_sol == NULL) {
		LOGD("g_sol == NULL, prepare data error!");
		return -1;
	}

	return Font_PrepareData(g_sol, code);
}

/*
 * Class:     cn_bxc_Stroke_BSData
 * Method:    getStrokeNum
 * Signature: (C)I
 */JNIEXPORT jint JNICALL Java_com_wiseman_writing_utility_BSJniApi_getStrokeNum(JNIEnv *env, jobject job) {
	if (g_sol == NULL) {
		LOGD("g_sol == NULL, BSJniApi_getStrokeNum error!");
		return -1;
	}

	return Font_GetStrokeNum(g_sol);
}

/*
 * Class:     cn_bxc_Stroke_BSData
 * Method:    getDrawPoint
 * Signature: (C)[I
 */JNIEXPORT jintArray JNICALL Java_com_wiseman_writing_utility_BSJniApi_getDrawPoint(JNIEnv *env, jobject job,
		jint fontSize) {
	int *intPoint = (int*) BNewMemory(STROKE_MAXPOINTNUM*2*sizeof(int));
	int i = 0;
	jintArray pointArray;
	if (Font_GetDrawPoint(g_sol, fontSize) == 0)
		return NULL;

	LOGD("Font_GetDrawPoint success!");

	g_sol->pCartoonInfo.pData[g_sol->pCartoonInfo.iDataSize] = 0;
	g_sol->pCartoonInfo.iDataSize++;

	for (i = 0; i < g_sol->pCartoonInfo.iDataSize; i++) {
		if (i % 2 == 0)
			intPoint[i] = (int) g_sol->pCartoonInfo.pData[i]
					>> POLYGON_FIX_SHIFT;
		else
			intPoint[i] = (int) g_sol->pCartoonInfo.pData[i];
	}

	pointArray = (*env)->NewIntArray(env, g_sol->pCartoonInfo.iDataSize);
	(*env)->SetIntArrayRegion(env, pointArray, 0, g_sol->pCartoonInfo.iDataSize,
			intPoint);

	BDeleteMemory(intPoint);

	return pointArray;
}

JNIEXPORT jintArray JNICALL Java_com_wiseman_writing_utility_BSJniApi_getFlagTab(JNIEnv *env, jobject job) {
	int arraySize = 0;
	jintArray pointArray;

	arraySize = g_sol->pCartoonInfo.SyncPointFlagTabSize;
	g_sol->pCartoonInfo.SyncPointFlagTab[arraySize] = 0;
	arraySize++;

	pointArray = (*env)->NewIntArray(env, arraySize);
	(*env)->SetIntArrayRegion(env, pointArray, 0, arraySize,
			g_sol->pCartoonInfo.SyncPointFlagTab);

	return pointArray;
}

JNIEXPORT void JNICALL  Java_com_wiseman_writing_utility_BSJniApi_unLoad(
		JNIEnv *env, jobject job) {
	ReleaseSOL();
	//solUninit(g_sol);
}

void ReleaseSOL() {

	if (g_sol == NULL) {
		return;
	}

	if (g_sol->pTabASCIIWidth) {
		LOGD("pTabASCIIWidth!");
		BDeleteMemory(g_sol->pTabASCIIWidth);
		g_sol->pTabASCIIWidth = NULL;
	}

	if (g_sol->pTabASCIIByCode) {
		LOGD("pTabASCIIByCode!");
		BDeleteMemory(g_sol->pTabASCIIByCode);
		g_sol->pTabASCIIByCode = NULL;
	}

	if (g_sol->pTabCodePageMemory) {
		LOGD("pTabCodePageMemory!");
		BDeleteMemory(g_sol->pTabCodePageMemory);
		g_sol->pTabCodePageMemory = NULL;
	}

	if (g_sol->pOneCharOrgData) {
		LOGD("pOneCharOrgData!");
		BDeleteMemory(g_sol->pOneCharOrgData);
		g_sol->pOneCharOrgData = NULL;
	}

	if (g_sol->hFile) {
		LOGD("hFile!");
		closeApp_file(g_sol->hFile);
		g_sol->hFile = NULL;
	}
	if (g_sol->pCartoonInfo.pData) {
		LOGD("pCartoonInfo.pData!");
		BDeleteMemory(g_sol->pCartoonInfo.pData);
		g_sol->pCartoonInfo.pData = NULL;
	}
	if (g_sol) {
		LOGD("pSOL!");
		BDeleteMemory(g_sol);
		g_sol = NULL;
	}
}

void UnicodeToAsc(USHORT* input, char* output) {
	int i = 0;
	while (output[i] = (char) input[i++])
		;
}

void AscToUnicode(char* input, USHORT* output) {
	int i = 0;
	while (output[i] = (USHORT) input[i++])
		;
}

JNIEXPORT jint JNICALL Java_com_wiseman_writing_utility_BSJniApi_getLanguage(JNIEnv *env, jobject job) {
	return solGetCodePage(g_sol);
}

